package com.shouhuyun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.shouhuyun.dao.MemberDao;
import com.shouhuyun.dao.OrderDao;
import com.shouhuyun.service.ReportService;
import com.shouhuyun.utils.DateUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营数据统计 服务
 * "data":{
 *          *      "todayVisitsNumber":0,
 *          *      "reportDate":"2019‐04‐25",
 *          *      "todayNewMember":0,
 *          *      "thisWeekVisitsNumber":0,
 *          *      "thisMonthNewMember":2,
 *          *      "thisWeekNewMember":0,
 *          *      "totalMember":10,
 *          *      "thisMonthOrderNumber":2,
 *          *      "thisMonthVisitsNumber":0,
 *          *      "todayOrderNumber":0,
 *          *      "thisWeekOrderNumber":0,
 *          *      "hotSetmeal":[
 *          *              {   "proportion":0.4545,
 *          *                  "name":"粉红珍爱(女)升级TM12项筛查体检套餐",
 *          *                  "setmeal_count":5
 *          *              },
 *          *              {  "proportion":0.1818,
 *          *                  "name":"阳光爸妈升级肿瘤12项筛查体检套餐",
 *          *                  "setmeal_count":2
 *          *              },
 *          *              {   "proportion":0.1818,
 *          *                  "name":"珍爱高端升级肿瘤12项筛查",
 *          *                  "setmeal_count":2
 *          *              },
 *          *              {   "proportion":0.0909,
 *          *                  "name":"孕前检查套餐",
 *          *                  "setmeal_count":1
 *          *              }
 *          *          ],
 *          *      },
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class  ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;  // 会员业务查询
    @Autowired
    private OrderDao orderDao; // 预约服务

    // 查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception {
        // 1 创建一个map存放数据
        Map<String,Object> map = new HashMap<>();

        // 动态获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        // 动态获得本月第一天日期
        String  firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        // 动态获得本周最后一天的日期
        String thisWeekLastDay = DateUtils.parseDate2String(DateUtils.getLastDayOfWeek(new Date()));
        // 动态获得本月最后一天日期
        String lastDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

        // 本周范围
        Map weekMap = new HashMap();
        weekMap.put("first",thisWeekMonday);
        weekMap.put("last",thisWeekLastDay);

        // 本月范围
        Map monthMap =  new HashMap();
        monthMap.put("first",firstDay4ThisMonth);
        monthMap.put("last",lastDay4ThisMonth);

        // 1.1 当前时间  展示在右上角  报表的产生日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
            // 1.2 本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
            // 1.3 总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
            // 1.4 本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            // 1.5 本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
            // 1.6 今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);
            // 1.7 本周预约数
//        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        Integer thisWeekOrderNumber = orderDao.findOrderBetween(weekMap);
            // 1.8 本月预约数
//        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        Integer thisMonthOrderNumber = orderDao.findOrderBetween(monthMap);
            // 1.9 今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);
            // 1.10 本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
            // 1.11 本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);


            //热门套餐（取前4）
        List<Map> hotSetmeal = orderDao.findHotSetmeal();


        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
